% \iffalse meta-comment --------------------------------------------------
% Copyright (C) 2021-2024 SJTUG
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software 
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
% ------------------------------------------------------------------------ \fi
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerouterthemesjtubeamer}[2024/12/21 v3.1.1 sjtubeamer outer theme]
%</package>
% \fi
% \CheckSum{0}
% \StopEventually{}
% \iffalse
%<*package>
% ------------------------------------------------------------------- \fi
%
% \subsection{Outer Theme}
%
% A \verb"beamer" outer theme dictates the style of the frame elements traditionally
% set outside the body of each slide: the head, footline, and frame title.
%
% Load SJTU VI Library to get the definition on color and shape.
%    \begin{macrocode}
\RequirePackage{sjtuvi}
%    \end{macrocode}
%
%
% \begin{macro}{\sjtubeamer@outer@nav}
%    \begin{macrocode}
\DefineOption{outer}{nav}{miniframes}
\DefineOption{outer}{nav}{infolines}
\DefineOption{outer}{nav}{sidebar}
\DefineOption{outer}{nav}{default}
\DefineOption{outer}{nav}{smoothbars}
\DefineOption{outer}{nav}{split}
\DefineOption{outer}{nav}{shadow}
\DefineOption{outer}{nav}{tree}
\DefineOption{outer}{nav}{smoothtree}
\ExecuteOptionsBeamer{miniframes}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sjtubeamer@outer@logopos}
%    \begin{macrocode}
\DefineOption{outer}{logopos}{topright}
\DefineOption{outer}{logopos}{bottomright}
\ExecuteOptionsBeamer{bottomright}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\ProcessOptionsBeamer
%    \end{macrocode}
%
% Enable compress option on beamer to avoid multiline navigation dots.
%    \begin{macrocode}
\beamer@compresstrue
%    \end{macrocode}
%
% \subsubsection{Frame Title}\label{sec:frametitle}
%
%
% Define the beamer template \verb"frametitle" to add a logo in the right of the
% frametitle (top right corner) for \verb"topright" option.
% If it is not \verb"topright" option, then the default \verb"frametitle" template is used.
%
% \verb"smoothbars", \verb"smoothtree" and \verb"shadow" theme use a different
% frame title insertion mechanism in \verb"beamer" class so that the redefinition
% on \verb"frametitle" should be done just after the document starts.
% This overwrites the original definition of those outer theme and may cause a
% certain degree of style lost.
%
% COPYRIGHT NOTICE: Some codes are the same as \verb"beamer" class under LPPL 1.3c license.
%    \begin{macrocode}
\if\EqualOption{outer}{logopos}{topright}
  \AtBeginDocument{
    \defbeamertemplate*{frametitle}{sjtubeamer}[1][]
    {%
      \ifbeamercolorempty[bg]{frametitle}{}{\nointerlineskip}%
      \@tempdima=\textwidth%
      \advance\@tempdima by\beamer@leftmargin%
      \advance\@tempdima by\beamer@rightmargin%
      \begin{beamercolorbox}[sep=0.15cm,leftskip=0.15cm,#1,wd=\the\@tempdima]{frametitle}
        \begingroup
        \usebeamerfont{frametitle}
        \vbox{}
%    \end{macrocode}
% Lift the left part a little bit.
%    \begin{macrocode}
        \ifx\insertframesubtitle\@empty\vskip-0.5ex%
        \else\vskip-0.7ex\fi%
        \if@tempswa\else\csname beamer@fte#1\endcsname\fi%
        \strut\insertframetitle\strut\par%
        {%
          \ifx\insertframesubtitle\@empty%
          \else%
          {
            \usebeamerfont{framesubtitle}
            \usebeamercolor[fg]{framesubtitle}
            \strut\insertframesubtitle\strut\par
          }%
          \fi
        }%
%    \end{macrocode}
% Move to the baseline of the text, but the adjustment is still not so perfect
% since we made a trade-off for macnew fontset and fandol fontset,
% where the former one is higher than the latter one.
%
% TODO: refactor the logo insertion, and define the baseline for the logo,
% and then make the two columns vertically centered without any parameter adjustment.
%    \begin{macrocode}  
        \vskip-1.07ex%
        \endgroup%
%    \end{macrocode}
% Start the right logo part.
% Lift the logo up, relative to the end position of the left part.
%    \begin{macrocode}
        \raggedleft%
        \begingroup
        \ifx\insertframesubtitle\@empty\vskip-2.2ex%
        \else\vskip-3.05ex\fi%
%    \end{macrocode}
% Insert the outer logo with 3ex height.
% \verb"\vphantom" here is to create a phantom box to make sure the
% \verb"\resizebox" has a non-zero height box input.
% The calculation is fixed for a 3ex height logo placeholder.
%    \begin{macrocode}
        \resizebox{!}{3ex}{\vphantom{-}\usebeamertemplate{logo}}\hspace*{10pt}%
        \endgroup%
        \ifx\insertframesubtitle\@empty%
        \else\vskip0.75ex\fi%
        \if@tempswa\else\vskip-.3cm\fi%
      \end{beamercolorbox}%
    }
  }
%    \end{macrocode}
% Make a larger height of frametitle when it is in \verb"default" navigation configuration.
% This could only be made when it is in \verb"topright" logo position mode.
%
% TODO: Make it more consistent when it is under \verb"bottomright" mode.
% The default behavior of \verb"beamer" class will make the top margin slightly
% larger than the bottom margin, which causes assymetry.
%    \begin{macrocode}
  \if\EqualOption{outer}{nav}{default}
    \AtBeginDocument{
      \setbeamertemplate{frametitle}[sjtubeamer][leftskip=0cm,sep=0.3cm]
    }
  \fi
\fi
%    \end{macrocode}
%
% \subsubsection{Execute Outer Theme}
%
% Use the built-in outer templates.
% If you want to take special care about one outer theme,
% please add a condition test on that theme.
% Otherwise, it will use the default configuration of the corresponding
% built-in outer theme.
%    \begin{macrocode}
\if\EqualOption{outer}{nav}{miniframes}
  \useoutertheme[footline=institutetitle]{miniframes}
%    \end{macrocode}
% Sidebar.
%    \begin{macrocode}
\else\if\EqualOption{outer}{nav}{sidebar}
    \useoutertheme{sidebar}
%    \end{macrocode}
% Since the logo area in the sidebar theme is square,
% the default configuration for \verb"\logo" will be \verb"\sjtubadge"
% no matter what kind of cover processed in the inner theme.
% If you want to modify the logo later, change the logo manually after loading the theme.
%    \begin{macrocode}
    \logo{\resizebox{!}{1cm}{\sjtubadge}}
%    \end{macrocode}
% And it will use the logo position configuration \verb"topright" for sidebar
% to remove the logo in the bottom right corner.
% The config will not be modified after the outer theme is loaded.
%    \begin{macrocode}
    \def\sjtubeamer@outer@logopos{topright}
%    \end{macrocode}
% Use the default template for \verb"frametitle" in sidebar,
% since there has already been a logo in the top left corner.
% The setup has to be done after document starts since the previous set of
% \verb"frametitle" template is also set up after document as is shown in
% Section \ref{sec:frametitle}.
% Appending on this hook will override the previous setup.
%    \begin{macrocode}
    \AtBeginDocument{
      \setbeamertemplate{frametitle}[sidebar theme]
    }
%    \end{macrocode}
% If it is the other theme, change the beamercolor to fit smooth* theme.
%    \begin{macrocode}
  \else
    \useoutertheme{\sjtubeamer@outer@nav}
    \setbeamercolor{title in head/foot}{use=structure,bg=white,fg=structure.fg}
  \fi\fi
%    \end{macrocode}
%
%   Color patch for default outer theme.
%    \begin{macrocode}
\if\EqualOption{outer}{nav}{default}
  \setbeamercolor{frametitle}{use=palette primary,
    bg=palette primary.bg,fg=palette primary.fg}
%    \end{macrocode}
% Since \verb"infolines" outer theme in \verb"beamer" class has redefined the
% beamer color below which doesn't confirm the standing-free principle,
% the redefined color will override after the outer theme is loaded.
%
% \verb"structure" beamer color stores the \verb"cprimary".
% Set the color relative to this for decoupling reasons.
%    \begin{macrocode}
\else\if\EqualOption{outer}{nav}{infolines}
  \setbeamercolor{author in head/foot}{use=structure,fg=white,bg=structure}
  \setbeamercolor{title in head/foot}{use=structure,fg=structure,bg=structure!10}
  \setbeamercolor{date in head/foot}{use=structure,fg=structure,bg=structure!20}
  \setbeamercolor{section in head/foot}{parent=author in head/foot}
  \setbeamercolor{subsection in head/foot}{parent=date in head/foot}
%    \end{macrocode}
% And \verb"infolines" also resize the text margin, we reset the margin to the
% default size in order to avoid unwanted shift in covers.
%    \begin{macrocode}
  \setbeamersize{text margin left=1cm,text margin right=1cm}
%    \end{macrocode}
% Color patch for sidebar, shadow, and smoothtrees.
%
% WARNING: This cannot be moved to the color theme, since the color was set in
% the outer theme itself. And the outer theme is the last theme to be loaded.
%    \begin{macrocode}
\else
  \setbeamercolor{frametitle}{use=titlelike,bg=white,fg=titlelike.fg}
  \setbeamercolor{frametitle right}{parent=subsection in head/foot}
\fi\fi
%    \end{macrocode}
%
% \subsubsection{Bottombar}
%
% Clear the original definition of sidebar first.
% Then append the page info to the footline,
% which could avoid collision on footnote.
%    \begin{macrocode} 
\setbeamertemplate{sidebar right}{}
%    \end{macrocode}
%
% Add page number to the toolbox.
%    \begin{macrocode}
\addtobeamertemplate{navigation symbols}{}{
  \hbox{
    \raisebox{1.2pt}[0pt][0pt]{
      \usebeamerfont{footline}
      \usebeamercolor{footline}
      \color{footline.fg!50}
      \insertframenumber/\inserttotalframenumber
      \hspace*{0.2em}
    }
  }
}
%    \end{macrocode}
%
% Append the navigation to the footline to avoid the collision with the navigation symbols.
% This will clear the original placeholder for the logo -- place it elsewhere.
% However, in the \verb"bottomright" mode, the logo size is not constrainted.
%
% The code is a bit redundent since we don't want it to check the condition every time
% making a bottom bar.
%    \begin{macrocode}
\if\EqualOption{outer}{logopos}{bottomright}
  \addtobeamertemplate{footline}{
    \hfill%
    \usebeamertemplate***{navigation symbols}%
    \llap{\raisebox{1pc}[0pt][0pt]{\insertlogo}}
    \hspace*{0.1cm}\par
    \vskip 4pt
  }{}
\else
  \addtobeamertemplate{footline}{
    \hfill%
    \usebeamertemplate***{navigation symbols}%
    \hspace*{0.1cm}\par
    \vskip 4pt
  }{}
\fi
%    \end{macrocode}
%
% And set the page number template to none.
%    \begin{macrocode}
\setbeamertemplate{page number in head/foot}{}
%    \end{macrocode}
%
%
% \iffalse
%</package>
% \fi
% \Finale
\endinput
